package com.vf.admin.utils.douyin;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

/*
 * 利用HttpClient进行post请求的工具类 
 */  
public class HttpClientUtil {  
    public String doPost(String url,Map<String,String> map,String charset){  
        HttpClient httpClient = null;
        HttpGet httpPost = null;
        String result = null;  
        try{  
            httpClient = new SSLClient();  
            httpPost = new HttpGet(url);
            //设置参数  
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();  
            while(iterator.hasNext()){  
                Entry<String,String> elem = (Entry<String, String>) iterator.next();  
                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
            }  
            if(list.size() > 0){  
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
//                httpPost.setEntity(entity);  
                httpPost.addHeader("Host","api.amemv.com"); 
                httpPost.addHeader("Connection","keep-alive"); 
                httpPost.addHeader("Cookie","odin_tt=f774a2e8a2e319031c621510fae1e9112f9b84a5cae34e6fc2ac0868cb7a91080e5542c252d7d687d6361662a067e1e7; sid_guard=ea6991be99cee16d3faf1ff28c5d55f8%7C1545138740%7C5184000%7CSat%2C+16-Feb-2019+13%3A12%3A20+GMT; uid_tt=1e88cc5560c85719458b3021825f9214; sid_tt=ea6991be99cee16d3faf1ff28c5d55f8; sessionid=ea6991be99cee16d3faf1ff28c5d55f8; install_id=56986543517; ttreq=1$cac0c9c22ef0f2c5061b5d0573bae3f6727ac059"); 
//                httpPost.addHeader("Accept-Encoding","gzip"); 
//                httpPost.addHeader("X-SS-REQ-TICKET","1546918804767"); 
                httpPost.addHeader("X-Tt-Token","00ea6991be99cee16d3faf1ff28c5d55f8d6a5de97be39c0f182d2339875a6e155541697e3874598d5dbcba2d73991bb5a54"); 
                httpPost.addHeader("sdk-version","1"); 
                httpPost.addHeader("X-SS-RS","0"); 
                httpPost.addHeader("User-Agent","com.ss.android.ugc.aweme/400 (Linux; U; Android 8.1.0; zh_CN; Redmi 6; Build/O11019; Cronet/58.0.2991.0)"); 
                httpPost.addHeader("X-Khronos","1546920855"); 
                httpPost.addHeader("X-Gorgon","01cc1b78f899167da7ae828e766dc15bdfe9c42c3aeba1469e"); 
                httpPost.addHeader("X-Pods",""); 
            }  
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){  
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){  
                    result = EntityUtils.toString(resEntity);
//                    System.out.println("result:"+result);
                }  
            }  
        }catch(Exception ex){  
            ex.printStackTrace();  
        }  
        return result;  
    }  
    
    public String doPostHuaTi(String url,Map<String,String> map,String charset){  
        HttpClient httpClient = null;
        HttpGet httpPost = null;
        String result = null;  
        try{  
            httpClient = new SSLClient();  
            httpPost = new HttpGet(url);
            //设置参数  
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();  
            while(iterator.hasNext()){  
                Entry<String,String> elem = (Entry<String, String>) iterator.next();  
                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
            }  
            if(list.size() > 0){  
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
//                httpPost.setEntity(entity);  
//                httpPost.addHeader("Host","api.amemv.com"); 
////                httpPost.addHeader("Accept-Encoding","gzip"); 
//                httpPost.addHeader("sdk-version","1"); 
//                httpPost.addHeader("Connection","Keep-Alive"); 
//                httpPost.addHeader("Cookie","odin_tt=ba4d9c6f74f6410bfda880d4d1e79bd1e1013b879ddb8b972e31298ba04266474449628037c44dc831c7f04984c998a8f2da4dfd3cb76e973d937884ae922369; qh[360]=1; install_id=64634128482; ttreq=1$d4f3b99e5065f5068f5d3558028d32c3b887f8a9");
//                httpPost.addHeader("X-Gorgon","0326560d4400979026daad79226c0271797dc857242aa3c75e47"); 
//                httpPost.addHeader("X-Khronos","1551434951");
//                httpPost.addHeader("User-Agent","okhttp/3.10.0.1");
//                httpPost.addHeader("X-Pods","");
                
//                httpPost.addHeader("Accept-Encoding","gzip"); 
                httpPost.addHeader("Host","api.amemv.com"); 
                httpPost.addHeader("sdk-version","1"); 
                httpPost.addHeader("Connection","Keep-Alive"); 
                httpPost.addHeader("Cookie","odin_tt=ba4d9c6f74f6410bfda880d4d1e79bd1e1013b879ddb8b972e31298ba04266474449628037c44dc831c7f04984c998a8f2da4dfd3cb76e973d937884ae922369; qh[360]=1; install_id=64634128482; ttreq=1$d4f3b99e5065f5068f5d3558028d32c3b887f8a9");
                httpPost.addHeader("X-Gorgon","03006cc044002f8c84f38d0933e0a9cd4c78470890c00f03b7af"); 
                httpPost.addHeader("X-Khronos","1551441293");
                httpPost.addHeader("User-Agent","okhttp/3.10.0.1");
                httpPost.addHeader("X-Pods","");
                
            }  
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){  
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){  
                    result = EntityUtils.toString(resEntity);
//                    System.out.println("result:"+result);
                }  
            }  
        }catch(Exception ex){  
            ex.printStackTrace();  
        }  
        return result;  
    }  
    
    public String doPostzhuye(String url,Map<String,String> map,String charset){  
        HttpClient httpClient = null;
        HttpGet httpPost = null;
        String result = null;  
        try{  
            httpClient = new SSLClient();  
            httpPost = new HttpGet(url);
            //设置参数  
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();  
            while(iterator.hasNext()){  
                Entry<String,String> elem = (Entry<String, String>) iterator.next();  
                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
            }  
            if(list.size() > 0){  
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
//                httpPost.setEntity(entity);  
                httpPost.addHeader("Host","api.amemv.com"); 
                httpPost.addHeader("Connection","Keep-Alive"); 
                httpPost.addHeader("Cookie","qh[360]=1; odin_tt=ba4d9c6f74f6410bfda880d4d1e79bd1e1013b879ddb8b972e31298ba04266474449628037c44dc831c7f04984c998a8f2da4dfd3cb76e973d937884ae922369; install_id=64634128482; ttreq=1$d4f3b99e5065f5068f5d3558028d32c3b887f8a9"); 
//                httpPost.addHeader("Accept-Encoding","gzip"); 
                httpPost.addHeader("sdk-version","1"); 
                httpPost.addHeader("X-Pods","be0732c993a80c298c8a8a7aba39c3e79ab7b04d"); 
                httpPost.addHeader("User-Agent","okhttp/3.10.0.1"); 
                httpPost.addHeader("X-Khronos","1551262825"); 
                httpPost.addHeader("X-Gorgon","033fc6604400079ab5e145d9a2bbfa411831d3456e01eaee2ad2");
                
                
            }  
            HttpResponse response = httpClient.execute(httpPost);
            if(response != null){  
                HttpEntity resEntity = response.getEntity();
                if(resEntity != null){  
                    result = EntityUtils.toString(resEntity);
//                    System.out.println("result:"+result);
                }  
            }  
        }catch(Exception ex){  
            ex.printStackTrace();  
        }  
        return result;  
    }  
    
    
    public static String doGet(String httpurl) {
        HttpURLConnection connection = null;
        InputStream is = null;
        BufferedReader br = null;
        String result = null;// 返回结果字符串
        try {
            // 创建远程url连接对象
            URL url = new URL(httpurl);
            // 通过远程url连接对象打开一个连接，强转成httpURLConnection类
            connection = (HttpURLConnection) url.openConnection();
            // 设置连接方式：get
            connection.setRequestMethod("GET");
            // 设置连接主机服务器的超时时间：15000毫秒
            connection.setConnectTimeout(15000);
            // 设置读取远程返回的数据时间：60000毫秒
            connection.setReadTimeout(60000);
            // 发送请求
            connection.connect();
            // 通过connection连接，获取输入流
            if (connection.getResponseCode() == 200) {
                is = connection.getInputStream();
                // 封装输入流is，并指定字符集
                br = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                // 存放数据
                StringBuffer sbf = new StringBuffer();
                String temp = null;
                while ((temp = br.readLine()) != null) {
                    sbf.append(temp);
                    sbf.append("\r\n");
                }
                result = sbf.toString();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            if (null != is) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            connection.disconnect();// 关闭远程连接
        }

        return result;
    }

    
    /**
     * 发送get请求
     * @param url    路径
     * @return
     */
    public static String httpGet(String url){
        //get请求返回结果
    	String jsonResult = null;
        try {
            HttpClient client = null;
			try {
				client = new SSLClient();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
            //发送get请求
            HttpGet request = new HttpGet(url);
            request.addHeader("Host","api.amemv.com"); 
            request.addHeader("Connection","keep-alive"); 
            request.addHeader("Cookie","odin_tt=f774a2e8a2e319031c621510fae1e9112f9b84a5cae34e6fc2ac0868cb7a91080e5542c252d7d687d6361662a067e1e7; sid_guard=ea6991be99cee16d3faf1ff28c5d55f8%7C1545138740%7C5184000%7CSat%2C+16-Feb-2019+13%3A12%3A20+GMT; uid_tt=1e88cc5560c85719458b3021825f9214; sid_tt=ea6991be99cee16d3faf1ff28c5d55f8; sessionid=ea6991be99cee16d3faf1ff28c5d55f8; install_id=56986543517; ttreq=1$cac0c9c22ef0f2c5061b5d0573bae3f6727ac059"); 
            request.addHeader("Accept-Encoding","gzip"); 
//            request.addHeader("X-SS-REQ-TICKET","1546918804767"); 
            request.addHeader("X-Tt-Token","00ea6991be99cee16d3faf1ff28c5d55f8d6a5de97be39c0f182d2339875a6e155541697e3874598d5dbcba2d73991bb5a54"); 
            request.addHeader("sdk-version","1"); 
            request.addHeader("X-SS-RS","0"); 
            request.addHeader("User-Agent","com.ss.android.ugc.aweme/400 (Linux; U; Android 8.1.0; zh_CN; Redmi 6; Build/O11019; Cronet/58.0.2991.0)"); 
            request.addHeader("X-Khronos","1546920855"); 
            request.addHeader("X-Gorgon","01cc1b78f899167da7ae828e766dc15bdfe9c42c3aeba1469e"); 
            request.addHeader("X-Pods",""); 
            HttpResponse response = client.execute(request);
 
            /**请求发送成功，并得到响应**/
            System.out.println("response.getStatusLine().getStatusCode():"+response.getStatusLine().getStatusCode());
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                /**读取服务器返回过来的json字符串数据**/
            	jsonResult = EntityUtils.toString(response.getEntity());
                /**把json字符串转换成json对象**/
            } else {
            }
        } catch (IOException e) {
        }
        return jsonResult;
    }
}